# "Credit Claiming by Labeling"
# Virginia Oliveros, Rebecca Weitz-Shapiro, and Matthew S. Winters
# Comparative Political Studies

# Calculating False-Discovery-Rate-Adjusted p-Values

library(tidyverse)
library(estimatr)

cc.data <- haven::read_dta("CreditClaiming_Replication.dta")

### Adjusted p-values for Table 2

models.tab2 <- lm_robust(cbind(mayor_idea_respond, distrib_unbiased, prog_corrupt, satisfy_respond,
 vote_respond) ~ name + biased + unbiased + biasedXname + unbiasedXname + partisan_match_control, 
 data = cc.data[cc.data$saw_box==1,])

models.output.tab2 <- broom::tidy(models.tab2)

# Eliminate the intercept and the partisan match variables from consideration and then 
 # extract the p-values for the remaining 25 estimates
models.output.tab2 %>% filter(term!="(Intercept)" & term!="partisan_match_control") %>%
  select("p.value") -> p.val.vec.tab2

p.val.vec.tab2 <- unlist(p.val.vec.tab2)

models.output.tab2 %>% filter(term!="(Intercept)" & term!="partisan_match_control") %>%
  unite("coef.name", c("outcome","term")) %>% select("coef.name") -> coef.names

coef.names <- unlist(coef.names)

cbind(coef.names, p.adjust(p.val.vec.tab2, method="BH"))
cbind(coef.names, p.adjust(p.val.vec.tab2, method="bonferroni"))

# Reviewing BH-adjusted p-values for Coefficients Where Unadjusted p-Values are p < 0.10 
 # Col (1): labeled - p = 0.16
 # Col (1): biased*labeled - p = 0.24
 # Col (4): unbiased - p < 0.01
 # Col (4): unbiased*labeled - p = 0.16
 # Col (5): unbiased - p < 0.01
 # Col (5): unbiased*labeled - p = 0.11

### Adjusted p-values for Table 3

models.tab3 <- lm_robust(cbind(mayor_propoor, mayor_prorespond, mayor_patron, mayor_buyvotes,
  mayor_corrupt) ~ name + biased + unbiased + biasedXname + unbiasedXname + 
  partisan_match_control,  data = cc.data[cc.data$saw_box==1,])

models.output.tab3 <- broom::tidy(models.tab3)

models.output.tab3 %>% filter(term!="(Intercept)" & term!="partisan_match_control") %>%
  select("p.value") -> p.val.vec.tab3

p.val.vec.tab3 <- unlist(p.val.vec.tab3)

cbind(coef.names, p.adjust(p.val.vec.tab3, method="BH"))

# Reviewing BH-adjusted p-values for Coefficients Where Unadjusted p-Values are p < 0.10 
 # Col (1): unbiased - p = 0.20
 # Col (1): unbiased*labeled - p = 0.23

# End of File